package com.example.model;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpDelete;
import org.apache.http.client.methods.HttpUriRequest;

import android.os.AsyncTask;

public class DatabaseAccess extends AsyncTask<DataAccessBundle, Integer, String> {

	HttpClient httpclient;

	public enum ExecutionType {
		CREATE, READ, UPDATE, DELETE
	}

	public DatabaseAccess() {
		this.httpclient = new DefaultHttpClient();
	}

	public String getData(DataAccessBundle[] dab) {

		HttpUriRequest request = dab[0].getRequest();
		ExecutionType requestType = dab[0].getRequestType();
		
		String responseSTR = null;

		if (requestType == ExecutionType.CREATE) {
			
			HttpPost postRequest = (HttpPost)request;
			
			BufferedReader in = null;
			try {
				HttpResponse response = this.httpclient.execute(postRequest);

				in = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
				StringBuffer sb = new StringBuffer("");
				String line = "";
				String NL = System.getProperty("line.separator");
				while ((line = in.readLine()) != null) {
					sb.append(line + NL);
				}
				in.close();
				// String page = sb.toString();
				// System.out.println(page);
				// responseSTR = page;
				responseSTR = sb.toString();

			} catch (ClientProtocolException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} finally {
				if (in != null) {
					try {
						in.close();
					} catch (IOException e) {
						e.printStackTrace();
					}
				}
			}
			
		} else if (requestType == ExecutionType.READ) {
			
			HttpGet getRequest = (HttpGet)request;

			BufferedReader in = null;
			try {
				HttpResponse response = this.httpclient.execute(getRequest);

				in = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
				StringBuffer sb = new StringBuffer("");
				String line = "";
				String NL = System.getProperty("line.separator");
				while ((line = in.readLine()) != null) {
					sb.append(line + NL);
				}
				in.close();
				// String page = sb.toString();
				// System.out.println(page);
				// responseSTR = page;
				responseSTR = sb.toString();

			} catch (ClientProtocolException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} finally {
				if (in != null) {
					try {
						in.close();
					} catch (IOException e) {
						e.printStackTrace();
					}
				}
			}

		} else if (requestType == ExecutionType.UPDATE) {

			HttpPost putRequest = (HttpPost)request;

			BufferedReader in = null;
			try {
				System.out.println("In DBA PUT!!!!");
				HttpResponse response = this.httpclient.execute(putRequest);
				
				in = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
				StringBuffer sb = new StringBuffer("");
				String line = "";
				String NL = System.getProperty("line.separator");
				while ((line = in.readLine()) != null) {
					sb.append(line + NL);
				}
				in.close();
				// String page = sb.toString();
				// System.out.println(page);
				// responseSTR = page;
				responseSTR = sb.toString();
				System.out.println(responseSTR);
				
			} catch (ClientProtocolException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} finally {
				if (in != null) {
					try {
						in.close();
					} catch (IOException e) {
						e.printStackTrace();
					}
				}
			}

		} else if (requestType == ExecutionType.DELETE) {
			
			HttpDelete deleteRequest = (HttpDelete)request;

			BufferedReader in = null;
			try {
				HttpResponse response = this.httpclient.execute(deleteRequest);
				
				in = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
				StringBuffer sb = new StringBuffer("");
				String line = "";
				String NL = System.getProperty("line.separator");
				while ((line = in.readLine()) != null) {
					sb.append(line + NL);
				}
				in.close();
				// String page = sb.toString();
				// System.out.println(page);
				// responseSTR = page;
				responseSTR = sb.toString();
				
			} catch (ClientProtocolException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} finally {
				if (in != null) {
					try {
						in.close();
					} catch (IOException e) {
						e.printStackTrace();
					}
				}
			}

		} else {
			// do nothing
		}

		return responseSTR;
	}

	@Override
	protected String doInBackground(DataAccessBundle... dab) {
		// call getData
		return getData(dab);
	}
	


}
